// YaP IRC - Options Form and Preferences Saving.
//Copyright (C) 2006  Pierre-Alain DAVID

//This program is free software; you can redistribute it and/or
//modify it under the terms of the GNU Lesser General Public
//License as published by the Free Software Foundation; either
//version 2.1 of the License, or (at your option) any later version.

//This library is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//Lesser General Public License for more details.

//You should have received a copy of the GNU Lesser General Public
//License along with this library; if not, write to the Free Software
//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA


using System;
using YaP.Library;
using YaP.Library.XmlModels;

namespace YaP.Forms
{
    /// <summary>
    /// Description of OptionsFrm.
    /// </summary>
    public partial class OptionsForm
    {
        // private ServerXMLIO ServerSet;
        private string NetworkFile;
        private string OptionFile;

        public OptionsForm()
        {
            //
            // The InitializeComponent() call is required for Windows Forms designer support.
            //
            InitializeComponent();

            //
            // TODO: Add constructor code after the InitializeComponent() call.
            //
            FillForm();

            //ServerSet = new ServerXMLIO("servers.xml");
            // ArrayList inner = ServerSet.DataSet2List();
        }

        private void FillForm()
        {
            OptionsFactory OptFct = new OptionsFactory();
            txtNick.Text = OptFct.CurrentConfig.NICK;
            txtAlt.Text = OptFct.CurrentConfig.ALT;
            txtMail.Text = OptFct.CurrentConfig.EMAIL;
        }

        private void FillServerList()
        {
            cmbServ.Items.Clear();
            ServerFactory ServFact = new ServerFactory();

            if (cmbGroup.SelectedItem != null)
            {
                foreach (ServerItem itm in (cmbGroup.SelectedItem as NetworkServerList).ServerList)
                {
                    cmbServ.Items.Add(itm);
                }
                if (cmbServ.Items.Count > 0)
                    cmbServ.SelectedItem = cmbServ.Items[0];
            }
        }

        private void FillGroupList()
        {
            cmbGroup.Items.Clear();
            ServerFactory ServFact = new ServerFactory();
            foreach (NetworkServerList nl in ServFact.CurrentServers.Items)
            {
                cmbGroup.Items.Add(nl);
            }
            //Auto-Selecting the first item
            if (cmbGroup.Items.Count > 0)
            {
                cmbGroup.SelectedIndex = 0;
                //MessageBox.Show(((NetworkServerList)cmbGroup.Items[0]).ToString());
            }
        }


        private void button3_Click(object sender, EventArgs e)
        {
        }

        private void button4_Click(object sender, EventArgs e)
        {
        }

        private void OptionsFrm_Load(object sender, EventArgs e)
        {
        }

        private void comboBox4_SelectedIndexChanged(object sender, EventArgs e)
        {
            FillServerList();
        }

        private void button9_Click(object sender, EventArgs e)
        {
            //ServersRoot root = ServerRead();
            InputBoxForm ib = new InputBoxForm();
            ib.FormPrompt = "toto";
            ib.FormCaption = "New Network";
            ib.DefaultValue = string.Empty;
            ib.ShowDialog();
            string newnet = ib.InputResponse;
            if (ib.OK)
            {
                Library.ServerFactory ServFact = new YaP.Library.ServerFactory();
                ServFact.AddGroup(newnet);
                ServFact.Save();
                FillGroupList();
                //ServersRoot.groupRow grouprow = root.group.NewgroupRow();           
                //grouprow.name = s;
                //root.group.AddgroupRow(grouprow);
                //ServerWrite(root);
            }
        }

        private void comboBox5_SelectedIndexChanged(object sender, EventArgs e)
        {
        }

        private void button10_Click(object sender, EventArgs e)
        {
            if (cmbGroup.SelectedItem != null)
            {
                NewServerForm frm = new NewServerForm(((NetworkServerList) cmbGroup.SelectedItem).NetworkName,
                                                      ((NetworkServerList) cmbGroup.SelectedItem).NetworkID);
                frm.ShowDialog();
                if (frm.Action == FormAction.Ok)
                {
                    FillServerList();
                }
            }
        }

        private void button11_Click(object sender, EventArgs e)
        {
            if (cmbServ.SelectedItem != null)
            {
                NewServerForm frm = new NewServerForm(((ServerItem) cmbServ.SelectedItem).ServerID,
                                                      ((NetworkServerList) cmbGroup.SelectedItem).NetworkID);
                frm.ShowDialog();
                if (frm.Action == FormAction.Ok)
                {
                    FillServerList();
                }
            }
        }

        private void button12_Click(object sender, EventArgs e)
        {
            //ServersRoot root = ServerRead();
            //ServDesc sv = (ServDesc)cmbServ.SelectedItem;
            //ServersRoot.serverRow sr = null;
            //for (int i = 0; i< root.server.Count; i++)
            //{
            //    if (root.server[i].display == sv.Name)
            //        sr = root.server[i];

            //}
            //if (sr != null)
            //    root.server.RemoveserverRow(sr);

            //ServerWrite(root);
            //FillServerList();
        }

        private void label10_Click(object sender, EventArgs e)
        {
        }

        private void label9_Click(object sender, EventArgs e)
        {
        }

        private void tabPage2_Enter(object sender, EventArgs e)
        {
            FillGroupList();
            FillServerList();
        }

        private void comboBox4_SelectionChangeCommitted(object sender, EventArgs e)
        {
            FillServerList();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Library.OptionsFactory OptionsFact = new Library.OptionsFactory();
            CONFIG cfg = new CONFIG();
            cfg.NICK = txtNick.Text;
            cfg.ALT = txtAlt.Text;
            cfg.EMAIL = txtMail.Text;
            cfg.PREVSERV = "irc.librenet.net";
            cfg.PREVPORT = "6667";
            OptionsFact.CurrentConfig = cfg;
        }

        private void button9_Click_1(object sender, EventArgs e)
        {
            ServerFactory srvfact = new ServerFactory();
            srvfact.RemoveGroup(((NetworkServerList) cmbGroup.SelectedItem).NetworkID);
            srvfact.Save();
            FillGroupList();
        }
    }
}